#!/usr/bin/env groovy
// @file     tarball
// @author   Mac Radigan
// @purpose  

package sys.archive

import java.text.*
import java.util.TimeZone

import sys.tools.*

public class TarballTool extends Tool {

  public String getName() {
    return "tarball"
  }

  public String getDescription() {
    return "Archive files."
  }

  public String run(String[] args) {
    def ant = new AntBuilder()
    def cli = new CliBuilder(usage: 'tarball -d <directory> [-h]')  
    cli.h(longOpt:'help',      argName:'usage information',        required:false, args:0, 'show usage information')  
    cli.d(longOpt:'directory', argName:'flyer id to be retreived', required:true,  args:1,  'directory to tar')  
    cli.z(longOpt:'zip',       argName:'zip',                      required:false, args:0,  'zip')  
    cli.a(longOpt:'revision',  argName:'revision',                 required:false, args:1,  'revision')  
    cli.r(longOpt:'archive',   argName:'archive',                  required:false, args:1,  'archive')  
    OptionAccessor opt = cli.parse(args); if(!opt) { return }  
    includes = "${opt.d}/***"
    excludes = ""
    def dateFormatter = new SimpleDateFormat( "yyyy-MM-dd" )
    dateFormatter.setTimeZone( TimeZone.getTimeZone( "GMT" ) )
    date = dateFormatter.format( new Date() )
    name = new File(opt.d).getName()
    if(opt.r) { rev = opt.r } else { rev = "a" }
    if( !opt.z ) {
      dest = "${name}-${date}-${rev}.tar.gz"
      base = "${opt.d}/.."
      ant.tar( tarfile:dest, compression:"gzip", longfile:"gnu", basedir:base, includes:includes, excludes:excludes )
      ant.checksum( file:dest, forceOverwrite:"yes" )
      if( opt.a ) {
        source = "${name}-${date}-${rev}.tar.gz"
        ant.move( file:source, todir:opt.a )
        source = "${name}-${date}-${rev}.tar.gz.MD5"
        ant.move( file:source, todir:opt.a )
      }
    } else {
      dest = "${name}-${date}-${rev}.zip"
      base = "${opt.d}/.."
      ant.zip( destfile:dest, basedir:base, includes:includes, excludes:excludes )
      ant.checksum( file:dest, forceOverwrite:"yes" )
      if( opt.a ) {
        source = "${name}-${date}-${rev}.zip"
        ant.move( file:source, todir:opt.a )
        source = "${name}-${date}-${rev}.zip.MD5"
        ant.move( file:source, todir:opt.a )
      }
    }
  }

}

/* *EOF* */
